import MySQL
import Visualization

display_tags = ['article', 'inproceedings', 'phdthesis', 'book', 'proceedings']


def display(start_year=1949, end_year=2017):
    sql = 'SELECT year, tag, COUNT(*) AS count FROM dblp_xml WHERE year>={start_year} AND year<={end_year} GROUP BY year, tag' \
        .format(start_year=start_year, end_year=end_year)
    rows = MySQL.query(sql)
    maps = {}
    for row in rows:
        year = row['year']
        tag = row['tag']
        count = row['count']
        if str(year) not in maps:
            maps[str(year)] = {'x': display_tags, 'y': [0] * len(display_tags)}
        for k in range(len(display_tags)):
            if display_tags[k] == tag:
                maps[str(year)]['y'][k] = count
    # print(maps)
    for key, value in maps.items():
        s = sum(value['y'])
        for k in range(len(display_tags)):
            value['y'][k] = value['y'][k] / s
    Visualization.draw(maps, save="C:\\Users\\sissors-lab\\PycharmProjects\\AQPExperiment\\figure\\dblp\\1.svg", title='Papers from 1997 to 2017')


if __name__ == '__main__':
    MySQL.init('dblp')
    display(1997, 2017)
